package dk.mirasola.systemtraining.bridgewidgets.shared.model.distributionfiltertree;

import dk.mirasola.systemtraining.bridgewidgets.shared.model.Distribution;

import java.util.List;

public final class DistributionFilterGroup extends DistributionFilterGroupItem {
    private List<GroupItem> items = new ChildList<GroupItem>(this);
    private String name = "new group";

    public DistributionFilterGroup() {
    }

    public DistributionFilterGroup(String name) {
        setName(name);
    }

    public String getName() {
        return name;
    }

    /**
     * @param name
     * @throws IllegalArgumentException if name is null or more than 100 characters
     */
    public void setName(String name) {
        if (name == null || name.length() == 0 || name.length() > 100) {
            throw new IllegalArgumentException("null isn't a legal name");
        }
        if (name.length() > 200) {
            throw new IllegalArgumentException("name can't be longer than 200 characters");
        }
        this.name = name;
    }

    public List<GroupItem> getGroupItems() {
        return items;
    }

    protected boolean matchesInternal(Distribution distribution) {
        if (items.size() == 0) {
            return true;
        }

        DistributionFilterGroupItem cgi = null;
        LogicOperatorGroupItem ogi = null;
        int i = 0;
        while (i < items.size()) {
            cgi = (DistributionFilterGroupItem) items.get(i++);
            boolean distApproved = cgi.matches(distribution);

            if (i == items.size()) {
                // last
                return distApproved;
            } else {
                // not last
                ogi = (LogicOperatorGroupItem) items.get(i++);
                if (distApproved && ogi.getLogicOperator().equals(LogicOperator.OR)) {
                    return true;
                }
                if (!distApproved && ogi.getLogicOperator().equals(LogicOperator.AND)) {
                    return false;
                }
            }
        }
        throw new IllegalStateException();
    }

    @Override
    public String toString() {
        return "ConditonGroup(" + name + ")";
    }

    public boolean isCopy(GroupItem groupItem) {
        if (!super.isCopy(groupItem)) return false;
        if (!(groupItem instanceof DistributionFilterGroup)) return false;

        DistributionFilterGroup other = (DistributionFilterGroup) groupItem;
        if (!name.equals(other.name)) return false;
        if (items.size() != other.items.size()) return false;
        for (int i = 0; i < items.size(); i++) {
            if (!items.get(i).isCopy(other.items.get(i))) {
                return false;
            }
        }
        return true;
    }

    @Override
    public DistributionFilterGroup copy() {
        DistributionFilterGroup copy = new DistributionFilterGroup();
        copy.name = this.name;
        copy.setInverted(this.getInverted());

        for (GroupItem item : items) {
            copy.items.add(item.copy());
        }
        return copy;
    }
}
